#include"Task.hpp"
#include"RingQueue.hpp"
#include<pthread.h>
#include<memory>
#include<ctime>
#include<unistd.h>
#include<cstring>

using namespace std;

const char* ops="+-*/%";

void* consumerRoutine(void*args)
{
    RingQueue<Task>*rq=static_cast<RingQueue<Task>*>(args);

    while(true)
    {
        usleep(80000);
        Task t;
        rq->pop(t);

        t();
        cout<<"consumer done, 处理完成的任务是： "<<t.formatArg()<<t.formatRes()<<endl;
    }
}

void *producerRoutine(void*args)
{
    RingQueue<Task>*rq=static_cast<RingQueue<Task>*>(args);

    while(true)
    {
        usleep(100000);
        int x=rand()%100;
        int y=rand()%100;
        char op=ops[rand()%strlen(ops)];

        Task t(x,y,op);
        rq->push(t);

        cout << "productor done, 生产的任务是: " << t.formatArg() << "?" << endl;
    }
}

int main()
{
    srand(static_cast<unsigned int>(time(nullptr)));
    //环形队列
    RingQueue<Task>*rq=new RingQueue<Task>();

    pthread_t consumer;
    pthread_t producer;
    pthread_create(&consumer,nullptr,consumerRoutine,rq);
    pthread_create(&producer,nullptr,producerRoutine,rq);

    pthread_join(consumer,nullptr);
    pthread_join(producer,nullptr);

    return 0;
}